const axios = require('axios');
const moment = require("moment");

exports.getClientIP = async function (req) {
  let ip = req.headers['x-forwarded-for'] || req.headers['x-real-ip']

  let res = await axios.get(`https://restapi.amap.com/v3/ip?ip=${ip}&key=f5c6c858e6837995037681e7605676e7`)
  console.log("res======", ip);
  res.data.ip = ip;
  let rr = { ip, address: res.data.province + "" + res.data.city };
  return rr;
};


exports.uuid = function () {
  var guid = "a";
  for (var i = 1; i <= 31; i++) {
    var n = Math.floor(Math.random() * 16).toString(16);
    guid += n;
    if ((i == 8) || (i == 12) || (i == 16) || (i == 20)) {
      guid += "";
    }
  }
  guid += "";
  return guid;
}

exports.formatTime = function () {
  return moment().format("YYYY-MM-DD HH:mm:ss");
}
